package com.example.design.pattern.create

/**
 *Create by Fanketly on 2022/9/1
 *Gitee:https://gitee.com/fanketly
 *desc:
 */
fun main() {
    val deepCopy = DeepCopy()
    val clone = deepCopy.clone()
    println(deepCopy)
    println(clone)
    println(deepCopy.copyTest)
    println(clone.copyTest)

}

class CopyTest : Cloneable {
    public override fun clone(): Any {
        return super.clone()
    }
}

//浅拷贝 不拷贝类内的其他类,地址相同
class Copy : Cloneable {
    val copyTest = CopyTest()

    public override fun clone(): Any {
        return super.clone()
    }
}

class DeepCopy : Cloneable {
    var copyTest = CopyTest()

    public override fun clone(): DeepCopy {
        val clone = super.clone() as DeepCopy
        clone.copyTest = clone.copyTest.clone() as CopyTest
        return clone
    }
}